#include <string>
#include <fstream>
#include "contacts.pb.h"

void PrintContact(contactsname::contacts &contacts)
{
  
  for (int i = 0; i < contacts.contact_size();  ++i)
  {
      std::cout << "----------联系人" << i + 1 << "----------" << std::endl;
      const contactsname::PeopleInfo &people = contacts.contact(i);
      std::cout << "联系人姓名 : " << people.name() << std::endl;
      std::cout << "联系人年龄 : " << people.age() << std::endl;

      for (int i = 0; i < people.phone_size(); ++i)
      {
        const contactsname::PeopleInfo_Phone &phone = people.phone(i);
        std::cout << "联系人电话" << i + 1 << ": " << phone.number();

        //打印联系人电话
        std::cout << " ( " << phone.PhoneType_Name(phone.type()) << ")" << std::endl;
      }  
      
      //打印联系人地址
      if (people.has_data() && people.data().Is<contactsname::Adress>())
      {
        contactsname::Adress address;
        //获取联系人到address
        people.data().UnpackTo(&address);
        if (!address.home_address().empty())
        {
          std::cout << "联系人家庭地址：" << address.home_address() << std::endl;
        }

        if (!address.unit_address().empty())
        {
          std::cout << "联系人单元地址：" << address.unit_address() << std::endl;
           
        }
      }

      //打印qq或wechat联系方式
      switch(people.other_contact_case())
      {
        case contactsname::PeopleInfo::kQq :
            std::cout << "联系人qq:" << people.qq() << std::endl;
            break;
        case contactsname::PeopleInfo::kWechat :
            std::cout << "联系人 wecaht" << people.wechat() << std::endl;
            break;
        default:
            break;
      }
      
      //打印备注信息
      if (people.remark_size())
      {
        std::cout << "备注信息";
      }
      for (auto it = people.remark().cbegin(); it != people.remark().cend(); ++it)
      {
        std::cout << "  " << it->first << ": " << it->second << std::endl;
      }

  }

}
int main()
{
  contactsname::contacts contact;
  //读取本地存在的通讯录
  std::fstream input("contacts.bin", std::ios::in | std::ios::binary);
  if (!contact.ParseFromIstream(&input))
  {
    std::cerr << "read error" << std::endl;
    input.close();
    return -1;
  }

  PrintContact(contact);
  return 0;



  return  0;
}